<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { ref,onMounted  } from 'vue'
const teacherName = ref()
import { teacherInfoService, changePassword } from '@/api/teacherApi/teacherInfo.js'
const getTeacherInfo = async () => {
    let result = await teacherInfoService();
    //console.log(result)
    teacherName.value = result.data.name;
}
getTeacherInfo()
import { ElMessage, ElMessageBox } from 'element-plus'
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore();
import { useRouter } from 'vue-router';
const router = useRouter()
const logout = () => {
    //退出登录
    ElMessageBox.confirm(
        '你确认退出登录码？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            //清空pinia中的token和个人信息
            tokenStore.token = ''
            //跳转到登录页
            router.push('/')
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消退出',
            })
        })
}
const dialogFormVisible = ref(false)
const form = ref({
    password: ''
})
const changePass = () => {
    dialogFormVisible.value = true
};
const saveEdit = async () => {
    const data = {
        password: form.value.password
    }
    await changePassword(data)
        .then((res) => {
            if (res.code === 715) {
                ElMessage.success('修改成功！')
            } else {
                ElMessage.error('修改出錯！')
            }
        })
    dialogFormVisible.value = false

}
const defaultActive = ref('/teacherClassJoin');

onMounted(() => {
    // 在页面加载完成后，进行路由导航到 "/teacherClassJoin" 页面
    defaultActive.value = '/teacherClassJoin';
    // 也可以通过编程方式导航到指定路由
    router.push('/teacherClassJoin');
});
</script>

<template>
    <el-container class="layout-container">
        <el-header>
            <div>教师：<strong>{{ teacherName }}</strong> 你好</div>
            <p class="title_font">智能交互问答平台</p>
            <el-dialog title="教师修改密码" v-model="dialogFormVisible" :before-close="handleClose">
                <el-form :model="form">
                    <el-form-item label="新密码" :label-width="formLabelWidth">
                        <el-input v-model="form.password" autocomplete="off" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer" @click="dialogFormVisible = false">
                        <el-button>取消</el-button>
                        <el-button type="primary" @click="saveEdit">
                            确认
                        </el-button>
                    </span>
                </template>
            </el-dialog>
            <el-dropdown placement="bottom-end">
                <span class="el-dropdown__box">
                    <el-avatar :src="avatar" />
                    <el-icon>
                        <CaretBottom />
                    </el-icon>
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="logout" :icon="SwitchButton"
                            @click="changePass()">修改密码</el-dropdown-item>
                    </el-dropdown-menu>
                    <el-dropdown-menu>
                        <el-dropdown-item command="logout" :icon="SwitchButton"
                            @click="logout()">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </el-header>
        <div class="spacer"></div>
        <el-container>
            <!-- 左侧菜单 -->
            <el-aside>
                <!-- <div class="el-aside__logo"></div> -->
                <el-menu :default-active="defaultActive" active-text-color="#fff" background-color="#fff"
                    text-color="#000" router>
                    <el-menu-item index="/teacherClassJoin">
                        <span>加入的课堂</span>
                    </el-menu-item>
                    <el-menu-item index="/teacherClassCreate">
                        <span>创建的课堂</span>
                    </el-menu-item>
                    <el-menu-item index="/teacherCreateClass">
                        <span>创建课堂</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <!-- 右侧主区域 -->
            <el-container>
                <!-- 头部区域 -->

                <!-- 中间区域 -->
                <el-main>
                    <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                    内容展示区
                </div> -->
                    <router-view>

                    </router-view>
                </el-main>
                <!-- 底部区域 -->
                <el-footer>问答通</el-footer>
            </el-container>
        </el-container>

    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-menu-item.is-active {
        background-color: #0997F7 !important;
        /* 设置选中菜单项的背景颜色 */
    }

    .el-aside {
        background-color: #ffffff;
        width: 12%;

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px;
        width: 100%;
        background-color: #0997F7;
        color: #f5f5f5;
        font-size: large;
        height: 100px;

        .title_font {
            color: #f5f5f5;
            font-size: 40px;
            display: inline-block;
            margin-left: 40%;
            /* text-align: center; */
            position: absolute;
        }

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .spacer {
        height: 20px;
        /* 设置间隔的高度 */
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>